package io.zw.modules.xinpai.service.impl;

import io.zw.modules.xinpai.service.UserProjectService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.zw.common.utils.PageUtils;
import io.zw.common.utils.Query;

import io.zw.modules.xinpai.dao.UsersDao;
import io.zw.modules.xinpai.entity.UsersEntity;
import io.zw.modules.xinpai.service.UsersService;
import org.springframework.transaction.annotation.Transactional;


@Service("usersService")
public class UsersServiceImpl extends ServiceImpl<UsersDao, UsersEntity> implements UsersService {

    @Autowired
    private UserProjectService userProjectService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String phone= (String) params.get("mobile");
        String name= (String) params.get("name");
        Page<UsersEntity> page = this.selectPage(
                new Query<UsersEntity>(params).getPage(),
                new EntityWrapper<UsersEntity>().eq(StringUtils.isNotBlank((String) params.get("project_id")),"project_id",params.get("project_id")).eq("firm_id",params.get("firm_id"))
                        .like(StringUtils.isNotBlank(phone),"mobile",phone)
                        .like(StringUtils.isNotBlank(name),"name",name)

        );

        return new PageUtils(page);
    }

    @Override
    public List<Long> queryAreaList(Long userId) {
        return baseMapper.queryAreaList(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(UsersEntity users) {
        this.insert(users);
        //保存区域关系
        userProjectService.saveOrUpdate(users.getUId(),users.getAreaList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UsersEntity users){
        this.updateById(users);

        //保存区域关系
        userProjectService.saveOrUpdate(users.getUId(),users.getAreaList());
    }
}
